package com.psytech.app.task.service;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServiceLocator {

	private static final Logger LOG = Logger.getLogger(ServiceLocator.class);

	protected final transient ApplicationContext springContext = this.loadSpringContext();

	private ApplicationContext loadSpringContext() {
		return new ClassPathXmlApplicationContext("com/psytech/app/task/spring-bean.xml");
	}

	protected static ServiceLocator singleton = null;

	public interface ShortCut {
		ServiceLocator SL = ServiceLocator.getInstance();// NOPMD
	}

	protected ServiceLocator() {
		synchronized (ServiceLocator.class) {
			if (singleton != null)
				throw new IllegalStateException("SL singleton already instanciated.");
			singleton = this;
			LOG.debug("SL singleton instanciated with class: " + this.getClass().getName());
		}
	}

	public static ServiceLocator getInstance() {
		synchronized (ServiceLocator.class) {
			if (singleton == null)
				new ServiceLocator();
		}
		return singleton;
	}

	public final TaskManagerService getTaskManagerService() {
		return (TaskManagerService) springContext.getBean("taskManagerService");
	}
}
